package com.marvels.approve.common.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.marvels.approve.common.dao.McamLoanApplyInfoCommonDao;
import com.marvels.approve.common.service.McamLoanApplyInfoCommonService;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyMqProduct;
import com.marvels.common.dto.SynLoanStatus2ApplyDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class McamLoanApplyInfoCommonServiceImpl implements McamLoanApplyInfoCommonService {
	@Autowired
	private McamLoanApplyInfoCommonDao mcamLoanApplyInfoCommonDao;
	@Autowired
	private IMcamLoanApplyInfoDao mcamLoanApplyInfoDao;
	@Autowired
	private ApproveILoanApplyMqProduct loanApplyMqProduct;
	
//	@Autowired
//	private RabbitTemplate rabbitTemplate;
	
	@Value("${config.properties.path}")
	private  String configPropertiesPath;
	
	@Override
	public int updateMcamLoanApplyInfoStatusByLoanId(String loanId, String status,String originalState, String updatedBy)throws MarvelsBusinessException {
		if(StringUtils.isEmpty(loanId) || StringUtils.isEmpty(status) ){
			throw new MarvelsBusinessException("入参均不能为空");
		}
		//如果送入的originalState为空，取数据库查询状态
		McamLoanApplyInfoDTO loanApply = mcamLoanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		if(org.apache.commons.lang.StringUtils.isEmpty(originalState)) {
			originalState = loanApply.getStatus();
		}
		//如果为送修改人默认去缓存中拿
		if(org.apache.commons.lang.StringUtils.isEmpty(updatedBy)) {
			updatedBy = loanApply.getCustName();
		}
		MarvelsLogUtil.getInstance().info(">>>>>loanId:"+loanId+",修改进件状态开始");
		int count = mcamLoanApplyInfoCommonDao.updateMcamLoanApplyInfoStatusByLoanId(loanId, status, updatedBy);
		//同步到进件系统中记录全流程状态
		String acLoanStatusFundList=ServerProperties.getValue(configPropertiesPath, "resource.properties", "acLoanStatus.fund.list");
		String acLoanStatusProductList=ServerProperties.getValue(configPropertiesPath, "resource.properties", "acLoanStatus.product.list");
		MarvelsLogUtil.getInstance().info(loanId+"同步到进件系统中记录全流程状态acLoanStatusFundList:"+acLoanStatusFundList+",acLoanStatusProductList:"+acLoanStatusProductList);
		if(count > 0  && (StringUtil.isEmpty(acLoanStatusProductList) || acLoanStatusProductList.indexOf(","+loanApply.getProductType()+",")==-1) ) {//占时停止
			SynLoanStatus2ApplyDTO synStatus = new SynLoanStatus2ApplyDTO();
			synStatus.setLoanId(loanId);
			synStatus.setStatus(status);
			synStatus.setFundCode(loanApply.getFundCode());
			synStatus.setFundName(loanApply.getFundName());
			synStatus.setOriginalState(originalState);
			synStatus.setPrincipalAmtAct(loanApply.getPrincipalAmtAct());
			loanApplyMqProduct.synLoanStatus2Apply(synStatus);
		}
		return count;
	}
 

}
